package com.zmm.member.service.impl;

import com.zmm.member.dao.AuthTokenDao;
import com.zmm.member.dao.CustomerDao;
import com.zmm.member.model.AuthToken;
import com.zmm.member.model.Customer;
import com.zmm.member.service.CustomerService;
import com.zmm.util.time.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @author 900045
 * @description:
 * @name CustomerServiceImpl
 * @date By 2021-07-26 11:22:40
 */
@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {
	
	@Resource
	private CustomerDao customerDao;
	
	@Resource
	private AuthTokenDao authTokenDao;

	/**
	 * 新增 新增用户信息 -- 实现
	 * @author: 900045
	 * @date: 2021-07-26 11:25:25
	 * @throws 
	 * @param customer: 
	 * @return: boolean
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean addCustomer(Customer customer) {
		Integer saveCustomer = customerDao.saveCustomer(customer);
		if (saveCustomer > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 根据手机号 查询用户信息 -- 实现
	 * @author: 900045
	 * @date: 2021-07-26 16:08:44
	 * @throws 
	 * @param phone: 
	 * @return: com.zmm.member.model.Customer
	 **/
	@Override
	public Customer findOneCustomerByPhone(String phone) {
		return customerDao.selectOneCustomerByPhone(phone);
	}

	/**
	 * 根据手机号判断是否注册过 -- 实现
	 * @author: 900045
	 * @date: 2021-08-05 18:41:26
	 * @throws 
	 * @param phoneNum: 
	 * @return: boolean
	 **/
	@Override
	public boolean validatePhoneCertificate(String phoneNum) {
		Customer customer = customerDao.selectOneCustomerByPhone(phoneNum);
		if (ObjectUtils.isEmpty(customer)){
			// 不存在 ---> 注册
			return true;
		}
		//存在 ---> 登录
		return false;
	}

	/**
	 * 添加用户认证token -- 实现
	 * @author: 900045
	 * @date: 2021-08-06 16:40:29
	 * @throws 
	 * @param authToken: 
	 * @return: boolean
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean addAuthToken(AuthToken authToken) {
		authToken.setCreateTime(DateUtil.getNowTime());
		Integer save = authTokenDao.saveAuthToken(authToken);
		if (save > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 修改登录的IP -- 实现
	 * @author: 900045
	 * @date: 2021-08-06 17:29:47
	 * @throws 
	 * @param ip: 
	 * @param customerId: 
	 * @return: void
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void modifyLoginIpByCustomerId(String ip, Integer customerId) {
		customerDao.updateLoginInfo(customerId, ip, DateUtil.getNowTime());
	}
}
